/**
 * 
 */
package es.udc.emarketapp.http.view.actionForms;

import java.util.Arrays;
import java.util.Collection;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;

import es.udc.fbellas.j2ee.util.struts.action.PropertyValidator;
import es.udc.fbellas.j2ee.util.struts.action.DefaultActionForm;

public class FindOrdersForm extends DefaultActionForm {

    private static final long serialVersionUID = 1L;
    public final static String SELLER_IDENTIFIER = "SELLER_ID";
    public final static String ORDER_IDENTIFIER = "ORDER_ID";
    public final static String PAID_PENDENT_IDENTIFIER = "PAIDPENDENT_ID";
    public final static String PAID_PENDENT_AND_SELLER_IDENTIFIER = "PAIDPENDENT_SELLER_ID";
    public final static String SHIPPING_PENDENT_IDENTIFIER = "SHIPPINGPENDENT_ID";
    public final static String SHIPPING_PENDENT_AND_SELLER_IDENTIFIER = "SHIPPINGPENDENT_SELLER_ID";
    private final static Collection IDENTIFIER_TYPES = Arrays.asList(
            new String[]{SELLER_IDENTIFIER, ORDER_IDENTIFIER,
                PAID_PENDENT_IDENTIFIER, PAID_PENDENT_AND_SELLER_IDENTIFIER,
                SHIPPING_PENDENT_IDENTIFIER, SHIPPING_PENDENT_AND_SELLER_IDENTIFIER});
    private String identifier;
    private Long identifierAsLong;
    private String identifierType;
    private int startIndex;
    private int count;

    public FindOrdersForm() {
        reset();
    }

    public String getIdentifier() {
        return identifier;
    }

    public void setIdentifier(String identifier) {
        this.identifier = identifier.trim();
    }

    public String getIdentifierType() {
        return identifierType;
    }

    public void setIdentifierType(String identifierType) {
        this.identifierType = identifierType;
    }

    public Long getIdentifierAsLong() {
        return identifierAsLong;
    }

    public int getStartIndex() {
        return startIndex;
    }

    public void setStartIndex(int startIndex) {
        this.startIndex = startIndex;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public void reset(ActionMapping mapping, HttpServletRequest request) {
        reset();
    }

    public ActionErrors validate(ActionMapping mapping,
            HttpServletRequest request) {

        ActionErrors errors = new ActionErrors();
        PropertyValidator.validateMandatory(errors, "identifier", identifier);

        if (ORDER_IDENTIFIER.equals(identifierType)) {

            identifierAsLong = PropertyValidator.validateLong(errors,
                    "identifier", identifier, true, 1, Integer.MAX_VALUE);
        }

        return errors;
    }

    private void reset() {
        identifier = null;
        identifierAsLong = null;
        identifierType = null;
        startIndex = 1;
        count = 10;
    }
}
